#include "StdAfx.h"
#include "..\sgp\ClockManager.h"

namespace ja2{ namespace sgp
{
	ClockManager *ClockManager::m_pInstance = nullptr;

	ClockManager::ClockManager(HWND hWnd)
		: m_Window(hWnd)
	{
		m_pInstance = this;
		// Register the start time (use WIN95 API call)
		m_StartupTime = m_CurrentTime = GetTickCount();
		// Set timer handler
		SetTimer(m_Window, MAIN_TIMER_ID, 10, reinterpret_cast<TIMERPROC>(&ClockManager::callback));
	}


	ClockManager::~ClockManager(void)
	{
		// Make sure we kill the timer
		KillTimer(m_Window, MAIN_TIMER_ID);
	}

	void ClockManager::callback(HWND /*hWindow*/, UINT /*uMessage*/, UINT /*idEvent*/, DWORD dwTime)
	{
		// Update time
		m_pInstance->m_CurrentTime = dwTime;
		// Timer has wrapped, adjust time
		if (m_pInstance->m_CurrentTime < m_pInstance->m_StartupTime)
			m_pInstance->m_CurrentTime += (0xffffffff - m_pInstance->m_StartupTime);
		else
			m_pInstance->m_CurrentTime -= m_pInstance->m_StartupTime;
	}

	std::uint_least32_t ClockManager::clock() const
	{
		return m_CurrentTime;
	}
} /*sgp*/} /*ja2*/